﻿using SFML.Graphics;
using SFML.System;

namespace WolfCarol.SFU
{
    public enum Layout
    {
        LeftTop = 0,
        Top = 1,
        RightTop = 2,
        Left = 3,
        Center = 4,
        Right = 5,
        LeftBottom = 6,
        Bottom = 7,
        RightBottom = 8
    }

    public static class WindowLayoutUtility
    {
        public static void SetOrigin(this Transformable transform, Vector2f size, Vector2f coord)
        {
            transform.Origin = new Vector2f(size.X * coord.X, size.Y * coord.Y);
        }

        public static void SetPosition(this Transformable transform, Vector2f coord)
        {
            transform.Position = new Vector2f(Window.Instance.Width * coord.X, Window.Instance.Height * coord.Y);
        }

        public static void SetOrigin(this Transformable transform, Vector2u size, Layout layout)
        {
            switch (layout)
            {
                case Layout.LeftTop:
                    transform.Origin = new Vector2f(0f, 0f);
                    break;
                case Layout.Top:
                    transform.Origin = new Vector2f(size.X / 2f, 0f);
                    break;
                case Layout.RightTop:
                    transform.Origin = new Vector2f(size.X, 0f);
                    break;
                case Layout.Left:
                    transform.Origin = new Vector2f(0f, size.Y / 2f);
                    break;
                case Layout.Center:
                    transform.Origin = new Vector2f(size.X / 2f, size.Y / 2f);
                    break;
                case Layout.Right:
                    transform.Origin = new Vector2f(size.X, size.Y / 2f);
                    break;
                case Layout.LeftBottom:
                    transform.Origin = new Vector2f(0f, size.Y);
                    break;
                case Layout.Bottom:
                    transform.Origin = new Vector2f(size.X / 2f, size.Y);
                    break;
                case Layout.RightBottom:
                    transform.Origin = new Vector2f(size.X, size.Y);
                    break;
                default:
                    throw new ArgumentException("Invalid Layout.");
            }
        }

        public static void SetPosition(this Transformable transform, Layout layout)
        {
            switch (layout)
            {
                case Layout.LeftTop:
                    transform.Position = new Vector2f(0f, 0f);
                    break;
                case Layout.Top:
                    transform.Position = new Vector2f(Window.Instance.Width / 2f, 0f);
                    break;
                case Layout.RightTop:
                    transform.Position = new Vector2f(Window.Instance.Width, 0f);
                    break;
                case Layout.Left:
                    transform.Position = new Vector2f(0f, Window.Instance.Height / 2f);
                    break;
                case Layout.Center:
                    transform.Position = new Vector2f(Window.Instance.Width / 2f, Window.Instance.Height / 2f);
                    break;
                case Layout.Right:
                    transform.Position = new Vector2f(Window.Instance.Width, Window.Instance.Height / 2f);
                    break;
                case Layout.LeftBottom:
                    transform.Position = new Vector2f(0f, Window.Instance.Height);
                    break;
                case Layout.Bottom:
                    transform.Position = new Vector2f(Window.Instance.Width / 2f, Window.Instance.Height);
                    break;
                case Layout.RightBottom:
                    transform.Position = new Vector2f(Window.Instance.Width, Window.Instance.Height);
                    break;
                default:
                    throw new ArgumentException("Invalid Layout.");
            }
        }

        public static void SetOriginAndPosition(this Transformable transform, Vector2u size, Layout layout)
        {
            transform.SetOrigin(size, layout);
            transform.SetPosition(layout);
        }
    }
}
